$(document).ready(function () {
    //tab-dept页面加载完成后请求页面分页数据
    load("dept", 1, "firstPage")
});

function page(type, currentpage, ptype) {
    load(type, currentpage, ptype);
}

function goPage(type, ptype) {
    var currentPage = $("#gopage").val();
    load(type, currentPage, ptype);
}

function load(type, currentpage, ptype) {
    $.ajax({
        url: "/ListServletAjax",//请求地址
        // data: "type=dept&currentpage=1&ptype=firstPage",
        data: {type: type, currentpage: currentpage, ptype: ptype, nocache: new Date().getTime()},
        dataType: "json",
        async: true,//是否是异步请求
        type: 'POST',
        // contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (result) {
            //获取部门列表数据
            var depts = result.deptsStr;
            console.log(depts);
            var trHtml = "";
            var pageHtml = "";
            $.each(depts, function (n, value) {
                console.log(value.id);
                trHtml += "<tr>"
                trHtml += "<td width=\"3%\" height=\"22\" background=\"${path}/pages/tab/images/bg.gif\"";
                trHtml += "bgcolor=\"#FFFFFF\">";
                trHtml += "<div align=\"center\">";
                trHtml += "<input type=\"checkbox\" name=\"checkbox\" value=\"checkbox\"/>";
                trHtml += "</div>";
                trHtml += "</td>";
                trHtml += "<td width=\"3%\" height=\"22\" background=\"${path}/pages/tab/images/bg.gif\" bgcolor=\"#FFFFFF\">";
                trHtml += " <div align=\"center\"><span class=\"STYLE1\">" + (n + 1) + "</span></div>";
                trHtml += "</td>"
                trHtml += "<td width=\"12%\" height=\"22\" background=\"${path}/pages/tab/images/bg.gif\" bgcolor=\"#FFFFFF\">"
                trHtml += "<div align=\"center\"><span class=\"STYLE1\">" + value.deptName + "</span></div>";
                trHtml += "</td>";
                trHtml += "<td width=\"18%\" background=\"${path}/pages/tab/images/bg.gif\" bgcolor=\"#FFFFFF\">";
                trHtml += "<div align=\"center\"><span class=\"STYLE1\">" + value.deptManage + "</span></div>";
                trHtml += "</td>"
                trHtml += "<td width=\"23%\" height=\"22\" background=\"${path}/pages/tab/images/bg.gif\" bgcolor=\"#FFFFFF\">";
                trHtml += "<div align=\"center\"><span class=\"STYLE1\">" + value.deptDiscripte + "</span></div>";
                trHtml += "</td>";
                trHtml += "<td width=\"15%\" height=\"22\" background=\"${path}/pages/tab/images/bg.gif\" bgcolor=\"#FFFFFF\">";
                trHtml += "<div align=\"center\"><span class=\"STYLE1\">" + value.regDate + "</span></div>";
                trHtml += "</td>";
                trHtml += "<td width=\"15%\" height=\"22\" background=\"${path}/pages/tab/images/bg.gif\" bgcolor=\"#FFFFFF\" class=\"STYLE1\">";
                trHtml += "<div align=\"center\">基本操作</div>";
                trHtml += "</td>";
                trHtml += "</tr>";
            });
            // $("#deptDataTable").append(trHtml);//在table最后面添加一行
            // $("#deptDataTable tr:last").after(trHtml);//插入新行，在表格最后的位置
            $("#deptDataTable tr:not(:eq(0))").remove();
            $("#deptDataTable tr:eq(0)").after(trHtml);
            // $("#table3 tr:gt(0):eq(1)").after(newRow);//gt：（取大于指定索引的元素）;eq：（取指定索引的元素）
            // for (var i = 0; i < depts.length; i++) {
            //     for (var key in depts[i]) {
            //         console.log((key + ':' + depts[i][key]));
            //     }
            // }
            //
            // for (var p in depts) {
            //     console.log(depts[p].id);
            // }

            //console.log(depts);
            //var result1 = $.parseJSON(result);
            //console.log(result1);
            //console.log(result1.deptsStr[0].id);
            pageHtml += "<tr>";
            pageHtml += "<td class=\"STYLE4\">&nbsp;&nbsp;共有" + result.totalRecord + "条记录，当前第" + result.currentPage + "/" + result.totalPage + " 页</td>";
            pageHtml += "<td>"
            pageHtml += "<table border=\"0\" align=\"right\" cellpadding=\"0\" cellspacing=\"0\">";
            pageHtml += "<tr>"
            pageHtml += "<td width=\"40\"><a href=\"javascript:void(0)\" onclick=\"page('dept'," + result.currentPage + ",'firstPage')\"><img src=\"/pages/tab/images/first.gif\" width=\"37\" height=\"15\"/></a></td>"
            pageHtml += "<td width=\"45\"><a href=\"javascript:void(0)\" onclick=\"page('dept'," + result.currentPage + ",'prePage')\"><img src=\"/pages/tab/images/back.gif\" width=\"43\" height=\"15\"/></a></td>";
            pageHtml += "<td width=\"45\"><a href=\"javascript:void(0)\" onclick=\"page('dept'," + result.currentPage + ",'nextPage')\"><img src=\"/pages/tab/images/next.gif\" width=\"43\" height=\"15\"/></a></td>";
            pageHtml += "<td width=\"40\"><a href=\"javascript:void(0)\" onclick=\"page('dept'," + result.currentPage + ",'endPage')\"><img src=\"/pages/tab/images/last.gif\" width=\"37\" height=\"15\"/></a></td>";
            pageHtml += "<td width=\"100\"><div align=\"center\"><span class=\"STYLE1\"> 转到第 <input name=\"gopage\" id=\"gopage\" type=\"text\"";
            pageHtml += "onkeyup=\"if(this.value.length==1){this.value=this.value.replace(\/[\^1\-9]\/g,'')}else{this.value=this.value.replace(\/\\D\/g,'')}\"";
            pageHtml += "onafterpaste=\"if(this.value.length==1){this.value=this.value.replace(\/[\^1\-9]\/g,'')}else{this.value=this.value.replace(\/\\D\/g,'')}\"";
            pageHtml += "style=\"height:12px; width:20px; border:1px solid #999999;\"/>";
            pageHtml += "页</span></div>";
            pageHtml += "</td>";
            pageHtml += "<td width=\"40\"><a href=\"javascript:void(0)\" onclick=\"goPage('dept','pageJump')\"><img src=\"/pages/tab/images/go.gif\" width=\"37\"height=\"15\"/></a></td>";
            pageHtml += "</tr>";
            pageHtml += "</table>";
            pageHtml += "</td>";
            pageHtml += "</tr>";
            $("#pageTable tr:eq(0)").remove();
            $("#pageTable").append(pageHtml);
        },
        error: function (xhr) {

        }
    });
}